package com.chaoyi.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.chaoyi.model.KoreanVideoDetail;
import com.chaoyi.model.Topic;
import com.chaoyi.service.TopicService;

@Controller
@RequestMapping("/topic")
public class TopicController {
	@Resource
	private TopicService topicService;
	
	
    @RequestMapping("/list")
    public ModelAndView list(HttpServletRequest request, HttpServletResponse response){
        ModelAndView mod =new ModelAndView("/topic/topicList");
        return mod;
    }
    
	@RequestMapping("topicList")
	@ResponseBody
	public JSONObject topicList(HttpServletRequest request, HttpServletResponse response) {
		JSONObject json = new JSONObject();
		String offset = request.getParameter("offset");
		String limit = request.getParameter("limit");
		String topicType = request.getParameter("topicType");
		List<Topic> list = topicService.findList(offset, limit,topicType);
		int count =topicService.findCount(topicType);
		json.put("rows", list);
		json.put("total", count);
		return json;
	}
	
	
    /**
     *  新增页面
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("addTopic")
    public ModelAndView addTopic(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView mod = new ModelAndView("/topic/addTopic");
        return mod;
    }
    
    
    @ResponseBody
    @RequestMapping("addTopicSave")
    public JSONObject addTopicSave(Topic entity) {
        JSONObject json = new JSONObject();
        int number = topicService.saveTopic(entity);
        if (number == 1) {
            json.put("missage", "success");
        } else {
            json.put("missage", "error");
        }
        return json;
    }
    
    @ResponseBody
    @RequestMapping("deleteTopic")
    public JSONObject deleteTopic(HttpServletRequest request, HttpServletResponse response, int id) {
        JSONObject json = new JSONObject();
        int number = topicService.deleteTopic(id);
        if (number == 1) {
            json.put("missage", "success");
        } else {
            json.put("missage", "error");
        }
        return json;
    }
    
    @RequestMapping("addTopicDetail")
    public ModelAndView addTopicDetail(int id,HttpServletRequest request, HttpServletResponse response) {
        ModelAndView mod = new ModelAndView("/topic/topicDetail");
        Topic obj = topicService.findTopicById(id);
        mod.addObject("obj",obj);
        return mod;
    }
    
    
    @RequestMapping("topicDeatilSave")
    @ResponseBody
    public JSONObject topicDeatilSave(Topic entity) {
        JSONObject json = new JSONObject();
        int number = topicService.topicDeatilSave(entity);
        if (number == 1) {
            json.put("missage", "success");
        } else {
            json.put("missage", "error");
        }
        return json;
    }
}
